/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import config.Debug;
import config.Helper;
import config.Settings;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class PlaylistPage {
	private int id;
	private String name;

	private List<Container> containers = new ArrayList<Container>();
	private boolean isPlaying = false;

	// private Map<PlaylistRecord, Container> recordMap = new
	// TreeMap<PlaylistRecord, Container>();

	public PlaylistPage(int id) {
		this.createPlaylistPageFromDatabaseById(id);
		this.generateContents();
	}

	private void createPlaylistPageFromDatabaseById(int id) {
		String sql = String.format("SELECT * FROM `page` WHERE `page_id`='%1$s';", id);
		Connection conn = Settings.getLocalDatabaseConnection();
		ResultSet result = null;
		Statement stmt = null;
		try {
			int numResults = Helper.getNumberOfResults(sql);
			if (numResults <= 0) {
				// throw new
				// com.mysql.jdbc.exceptions.MySQLDataException("There is no page with id: "
				// + id);
				Debug.log("There is no page with id: " + id, Debug.WARNING_LEVEL);
				return;
			}
			stmt = conn.createStatement();
			result = stmt.executeQuery(sql);
			result.first();
			this.id = id;
			this.name = result.getString("page_name");
		} catch (SQLException ex) {
			Logger.getLogger(Playlist.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException ex) {
				Logger.getLogger(PlaylistPage.class.getName()).log(Level.SEVERE, null, ex);
			}

		}
	}

	/*
	 * public Map<PlaylistRecord, Container> createRecordMapping() {
	 * if(this.containers==null || this.containers.size()==0) return null;
	 * Map<PlaylistRecord, Container> map = new TreeMap<PlaylistRecord,
	 * Container>(); for(Container c : this.containers) { for(PlaylistRecord pr
	 * : c.getRecords()) map.put(pr, c); } return map; }
	 * 
	 * public Map<PlaylistRecord, Container> getRecordMap() { return recordMap;
	 * }
	 */

	private void generateContents() {
		String sql = String.format("SELECT DISTINCT `container_id` FROM `playlist_record` WHERE `page_id`='%1$s' and `active`=1;", this.id);
		Connection conn = Settings.getLocalDatabaseConnection();
		ResultSet result = null;
		Statement stmt = null;
		try {
			int numResults = Helper.getNumberOfResults(sql);
			if (numResults <= 0) {
				// there are no records
				return;
			}
			stmt = conn.createStatement();
			result = stmt.executeQuery(sql);
			result.beforeFirst();
			while (result.next() == true) {
				int containerId = result.getInt("container_id");
				Container c = new Container(containerId);
				this.containers.add(c);
			}
		} catch (SQLException ex) {
			Logger.getLogger(Playlist.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException ex) {
				Logger.getLogger(PlaylistPage.class.getName()).log(Level.SEVERE, null, ex);
			}

		}
		// this.recordMap = this.createRecordMapping();
	}

	@Override
	public String toString() {
		return String.format("[Playlist Page:%1$d] - %2$s, containers: %3$d", this.id, this.name, this.containers.size());

	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public static void main(String[] args) {
		PlaylistPage p = new PlaylistPage(19);
		System.out.println(p.toString());
	}
}
